/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
#include "celpfilt.h"
#include <stdio.h>
#include <assert.h>

void do_pfilt_dynamic32to16(
  FILTER        *flt,
  fxpt_16       coef[],         /*  2.13 format */
  fxpt_32       indata[],       /* 16.15 format */
  fxpt_16       outdata[])      /* 15.0  format */
{
  int i, j;
  int64 acc;
  fxpt_32 *memory;
  fxpt_32 *pm;
  fxpt_32 *pc;
  fxpt_32 coef32[11];

  ASSERT(flt->data_length == 60);
  ASSERT(flt->order == 10);

  for (i=0 ; i<11 ; i++)
	  coef32[i]= coef[i];

  if (coef[0] != 8192) {                /* 1.0 in 2.13 format */
    CELP_PRINTF(("polefilt:  bad coefficients\n"));
    CELP_ABORT();
  }

  memory = flt->memory;
	DUMPARR(flt->memory,15,flt->order)
	DUMPARR(indata,15,flt->data_length)

  for (i = 0; i < 60; i++) {

    acc = indata[i];
    acc <<= 13;

    for (j = 9, pm= memory+10, pc=coef32+10; j ; j--,pm--,pc--) {
      acc -= (fxpt_64) *pm * *pc;
      pm[0] = pm[-1];
    }

    acc -= (int64)coef32[1] * memory[1];
//    acc >>= 13;
    memory[1] =  fxpt_saturate32_round(acc,13);

//	DUMPARR(flt->memory,13,10)

    outdata[i] = fxpt_saturate16(fxpt_saturate32_round(acc,28));

  }
  memory[0] =  fxpt_saturate32_round(acc,13);

DUMPARR(flt->memory,15,flt->order)
	DUMPARR0(outdata,flt->data_length)
}
